package com.example.hcbmsm.service.impl;

import com.example.hcbmsm.domain.Ai;
import com.example.hcbmsm.domain.AiExample;
import com.example.hcbmsm.mapper.AiMapper;
import com.example.hcbmsm.service.AiService;
import com.example.hcbmsm.util.HCBMSMResult;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Xinghm
 * @version 1.0
 * @since 2024/11/5 19:46
 */
@Service
public class AiServiceImpl implements AiService {
    @Autowired
    private AiMapper aiMapper;

    @Override
    public Map list(Long docId, int page, int limit) {
        Integer total = aiMapper.selectAiList(docId).size();
        PageHelper.startPage(page, limit);
        List<Ai> list = aiMapper.selectAiList(docId);
        Map map = new HashMap<>();
        map.put("total", total);
        map.put("data", list);
        return HCBMSMResult.SUCCESS(map);
    }

    @Override
    public Map add(Ai ai) {
        if (aiMapper.insertSelective(ai) == 1) {
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }

    @Override
    public Map renew(Ai ai) {
        ai.setModifyTime(new Date());
        if (aiMapper.updateByPrimaryKeySelective(ai) == 1) {
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }

    @Override
    public Map remove(Long docId) {
        AiExample aiExample = new AiExample();
        aiExample.createCriteria().andDocIdEqualTo(docId);
        if (aiMapper.deleteByExample(aiExample) == 1) {
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }
}
